var mongoose = require('mongoose');
var dishSchema = new mongoose.Schema({
	name:{
		type: String,
		required: true
	},
	description:{
		type: String
	},
	style:{
		type: String,
		required: true
	},
	modified:{
		type:Date,
		default: Date.now
	}
});

dishSchema.statics.findByID = function findByID(id, callBack){
	return this.find({'_id':id}).exec(callBack);
}

dishSchema.statics.createNewDish = function createNewDish(name, description, style, callBack){
	var dish = new Dish({
		name: name,
		description: description,
		style: style
	});
	dish.save(callBack);
	return dish;
}

dishSchema.methods.updateDish = function updateDish(name, description, style, callBack){
	if(name)
		this.name = name;
	if(description)
		this.description = description;
	if(style)
		this.style = style;

	this.modified = Date.now();
	this.save(callBack);
}

dishSchema.statics.deleteDish = function deleteDish(id, callBack){
	var dish = this.findOne({'_id':id}, function(err, dish){
		if(dish){
			dish.remove(function(err){
				if(err)
					console.log('error');
			});
		}
	});
}

var Dish = mongoose.model('Dish', dishSchema);

module.exports = Dish;